from datetime import datetime

from pydantic import BaseModel, Field
from typing import Optional, List

class EnvParameterCreate(BaseModel):
    battery: int = Field(..., description="Battery 外键 ID")
    price_ids: List[int] = Field(..., description="多个电价 ID")
    granularity: int
    initial_capacity: float
    period: int
    period_unit: str
    parameter_ids: List[int] = Field(..., description="多个参数 ID")
    name: str

class BatterySimple(BaseModel):
    battery_id: int
    battery_name: str

class PriceSimple(BaseModel):
    price_id: int
    price_type: int
    price_name: str

class ParameterSimple(BaseModel):
    parameter_id: int
    parameter_name: str
    icon: Optional[str] = None  # 允许为空

class BatterySimpleCategory(BaseModel):
    battery_id: int
    battery_name: str
    icon: Optional[str] = None  # 允许为空
    background: Optional[str] = None  # 允许为空

class PriceSimpleCategory(BaseModel):
    price_id: int
    price_type: int
    price_name: str
    icon: Optional[str] = None  # 允许为空
    background: Optional[str] = None  # 允许为空
    price_type: int

class ParameterSimpleCategory(BaseModel):
    parameter_id: int
    parameter_name: str
    icon: Optional[str] = None  # 允许为空
    background: Optional[str] = None  # 允许为空
    category_id: int



class EnvParameterOut(BaseModel):
    env_param_id: int
    granularity: int
    initial_capacity: float
    period: int
    period_unit: str
    battery: BatterySimple
    prices: List[PriceSimple]       # 改为复数，List 类型
    parameters: List[ParameterSimple]
    name: str

    class Config:
        from_attributes = True


class BatteryWithValues(BaseModel):
    battery_id: int
    battery_name: str
    max_output_power : float
    max_input_power : float
    capacity: float

class PriceWithValues(BaseModel):
    price_id: int
    price_type: int
    price_name: str
    time_list: List[datetime]
    value_list: List[float]


class ParameterWithValues(BaseModel):
    parameter_id: int
    parameter_name: str
    category_id: int
    icon: Optional[str] = None  # 允许为空
    time_list: List[datetime]
    value_list: List[float]


class EnvParameterDetailOut(BaseModel):
    env_param_id: int
    granularity: int
    period: int
    period_unit: str
    initial_capacity: float
    battery: BatteryWithValues
    prices: List[PriceWithValues]
    parameters: List[ParameterWithValues]

    class Config:
        from_attributes = True


class Category(BaseModel):
    category_id: int
    category_name: str

class CategoryOut(BaseModel):
    categories_parm: List[Category]
    categories_price: List[Category]
    prices: List[PriceSimpleCategory]
    parameters: List[ParameterSimpleCategory]
    batteries: List[BatterySimpleCategory]

    class Config:
        from_attributes = True


class SimpleItem(BaseModel):
    id: int
    original_id: int
    name: str
    icon: Optional[str] = None  # 允许为空
    background: Optional[str] = None  # 允许为空
    type: str  # 比如 "battery" / "price" / "parameter"
    category_id: Optional[int] = None  # 仅 parameter 用

class CategorizedItems(BaseModel):
    categorys: List[Category]
    items: List[SimpleItem]
